package com.erp.erp_ui.RoleGroup;

import com.alibaba.fastjson.JSONObject;
import com.erp.erp_entitys.LogDB.LogDBEntity;
import com.erp.erp_entitys.RoleGroup.RoleGroup2Entity;
import com.erp.erp_entitys.RoleGroup.RoleGroupEntity;
import com.erp.erp_entitys.RoleGroup.RoleGroupEntityExample;
import com.erp.erp_entitys.User.UserEntity;
import com.erp.erp_servers.RoleGroup.IRoleGroupService;
import com.erp.erp_servers.log.ILogDBService;
import com.erp.erp_ui.base.Base;
import com.erp.utils.R;
import com.erp.utils.ResultStatusEnum;
import com.erp.utils.WebApiUtils;
import com.ys.openapi.organization.department.Role;
import com.ys.openapi.organization.entity.ResultEntity;
import com.ys.openapi.organization.entity.RoleGroupVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @ClassName RoleGroupControl
 * @Description TODO
 * @Author songjizhen
 * @Date 2020-07-04 11:20
 * @Version 1.0
 */
@Controller
@RequestMapping(value = "rolegroup")
 public class RoleGroupControl extends Base {
    @Autowired
    IRoleGroupService roleGroupService;
    @Autowired
    ILogDBService logDBService;
    //云枢接口角色方法类
    Role role=new Role();
    /**
     * 新增
     * @param record
     * @return
     */
    @RequestMapping(value = "insert",method = RequestMethod.POST)
    @ResponseBody
     public R<Integer> insert(@RequestBody RoleGroupEntity record) {
        try {
            UserEntity user = (UserEntity)this.getBean("user");
            record.setCreator(user.getCode());
            record.setCreateTime(new Date());
            if(is_exit(record.getCode()))record.setCode(System.currentTimeMillis()+"");
            int num = roleGroupService.insertSelective(record);
            try {
                //开始与云枢同步
                RoleGroupVo groupVo=new RoleGroupVo();
                groupVo.setRoleGroupName(record.getName());
                groupVo.setCode(record.getCode());
                WebApiUtils wau=new WebApiUtils();
                Object result= role.add_role_group(groupVo,wau.getTokenString());
                //int resultCode=((JSONObject)result).getIntValue("errcode");
                logDBService.insert(new LogDBEntity(user.getName()+"("+user.getCode()+")","insert(新增)","角色组=>"+record.getName()));
                //同步结束
            }catch (Exception e){
                num =num + 10000;
            }
            return R.build(num);
        }catch (Exception e){
            e.printStackTrace();
            return R.buildError(ResultStatusEnum.ERROR);
        }
    }
    private Boolean is_exit(String code){
        RoleGroupEntity record=new RoleGroupEntity();
        record.setCode(code);
        List<RoleGroupEntity> roleGroupEntities = roleGroupService.selectByExample(record);
        return  roleGroupEntities.size()==0?false:true;
    }

    /**
     * 根据条件查询
     * @param
     * @return
     */
    @RequestMapping(value = "selectByPage")
    @ResponseBody
     public R<List<RoleGroupEntity>> selectByPage(RoleGroupEntity record){//@RequestBody RoleGroupEntity record,String pageNum,String pageSize
       try {
          // RoleGroupEntity record=new RoleGroupEntity();

           List<RoleGroupEntity> roleGroupEntities = roleGroupService.selectByExample(record);

           return R.build(roleGroupEntities);
       }catch (Exception e){
           e.printStackTrace();
           return R.buildError(ResultStatusEnum.ERROR);
       }
   }
    @RequestMapping(value = "selectAll")
    @ResponseBody
    public R<List<RoleGroupEntity>> selectAll(){//@RequestBody RoleGroupEntity record,String pageNum,String pageSize
        try {
            // RoleGroupEntity record=new RoleGroupEntity();
            RoleGroupEntity record=new RoleGroupEntity();
            List<RoleGroupEntity> roleGroupEntities = roleGroupService.selectByExample(record);

            return R.build(roleGroupEntities);
        }catch (Exception e){
            e.printStackTrace();
            return R.buildError(ResultStatusEnum.ERROR);
        }
    }
    @RequestMapping(value = "selectByCode")
    @ResponseBody
    public R<String> selectByCode(String  groupCode){
        try {
            String groupName="";
            RoleGroupEntity record=new RoleGroupEntity();
            record.setCode(groupCode);
            List<RoleGroupEntity> roleGroupEntities = roleGroupService.selectByExample(record);
            for(int i=0;i<roleGroupEntities.size();i++){
                if(groupCode.equals(roleGroupEntities.get(i).getCode())){
                    groupName=roleGroupEntities.get(i).getName();
                }
            }
            return R.build(groupName);
        }catch (Exception e){
            e.printStackTrace();
            return R.buildError(ResultStatusEnum.ERROR);
        }
    }
    @RequestMapping(value = "selectById")
    @ResponseBody
    public R<RoleGroupEntity> selectById(Integer id){
        try {
            RoleGroupEntity record=new RoleGroupEntity();
            record.setId(id);
            List<RoleGroupEntity> roleGroupEntities = roleGroupService.selectByExample(record);
            return R.build(roleGroupEntities.get(0));
        }catch (Exception e){
            e.printStackTrace();
            return R.buildError(ResultStatusEnum.ERROR);
        }
    }
    /**
     * 根据条件更新
     * @param record
     * @return
     */
    @RequestMapping(value = "update",method = RequestMethod.POST)
    @ResponseBody
     public R<Integer> update(@RequestBody RoleGroupEntity record){
        try {
            int num = roleGroupService.updateByExampleSelective(record);
            return R.build(num);
        }catch (Exception e){
            e.printStackTrace();
            return R.buildError(ResultStatusEnum.ERROR);
        }
    }

    /**
     * 根据条件删除
     * @param
     * @return
     */
    @RequestMapping(value = "delete")
    @ResponseBody
     public R<Integer> delete(String[] ids){//@RequestBody RoleGroupEntity record
        try {
            List<Integer> list=new ArrayList<Integer>();
            for(int i=0;i<ids.length;i++){
                list.add(Integer.parseInt(ids[i].replace("[","").replace("]","")));
            }
            RoleGroupEntityExample example=new RoleGroupEntityExample();
            RoleGroupEntityExample.Criteria criteria = example.createCriteria();
            criteria.andIdIn(list);
            int num = roleGroupService.deleteByExample(example);
            return R.build(num);
        }catch (Exception e){
            e.printStackTrace();
            return R.buildError(ResultStatusEnum.ERROR);
        }
    }

    /**
     * 获取角色树,角色组只允许一级
     * @return
     */
    @RequestMapping(value = "getRoleTree",method = RequestMethod.POST)
    @ResponseBody
     public R<List<RoleGroup2Entity>> getRoleTree(){
        try {
            List<RoleGroup2Entity> roleTree = roleGroupService.getRoleTree();
            return R.build(roleTree);
        }catch (Exception e){
            e.printStackTrace();
            return R.buildError(ResultStatusEnum.ERROR);
        }

    }

    /**
     * 判断名称是否存在
     * @param name
     * @return
     */
    @RequestMapping(value = "queryByName",method = RequestMethod.GET)
    @ResponseBody
    public R<Boolean> queryByName(String name){

        try {
            RoleGroupEntityExample example=new RoleGroupEntityExample();
            RoleGroupEntityExample.Criteria criteria = example.createCriteria();
            criteria.andNameEqualTo(name);
            List<RoleGroupEntity> roleGroupEntities = roleGroupService.selectByExample(example);

            return  R.build(roleGroupEntities.size()==0?false:true);
        }catch (Exception e){
            e.printStackTrace();
            return R.buildError(ResultStatusEnum.ERROR);
        }
    }
}
